package org.example.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.backend.model.Result;
import org.example.backend.model.WeatherAlert;
import org.example.backend.model.WeatherCurrent;
import org.example.backend.model.WeatherForecast;
import org.example.backend.service.WeatherService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weather")
@RequiredArgsConstructor
@Tag(name = "天气服务API", description = "天气信息查询相关接口")
public class WeatherController {

    private final WeatherService weatherService;

    @GetMapping("/current/{cityCode}")
    @Operation(summary = "获取城市当前天气", description = "根据城市代码获取当前天气信息")
    public Mono<Result<WeatherCurrent>> getCurrentWeather(
            @Parameter(description = "城市代码", required = true)
            @PathVariable String cityCode) {
        return weatherService.getCurrentWeather(cityCode);
    }

    @GetMapping("/current")
    @Operation(summary = "获取所有城市当前天气", description = "获取系统中所有城市的当前天气信息")
    public Mono<Result<List<WeatherCurrent>>> getAllCurrentWeather() {
        return weatherService.getAllCurrentWeather();
    }

    @GetMapping("/forecast/{cityCode}")
    @Operation(summary = "获取城市天气预报", description = "根据城市代码获取未来天气预报")
    public Mono<Result<List<WeatherForecast>>> getWeatherForecast(
            @Parameter(description = "城市代码", required = true)
            @PathVariable String cityCode) {
        return weatherService.getWeatherForecast(cityCode);
    }

    @GetMapping("/alert/{cityCode}")
    @Operation(summary = "获取城市天气警报", description = "根据城市代码获取天气警报信息")
    public Mono<Result<List<WeatherAlert>>> getWeatherAlert(
            @Parameter(description = "城市代码", required = true)
            @PathVariable String cityCode) {
        return weatherService.getWeatherAlert(cityCode);
    }

    @GetMapping("/all/{cityCode}")
    @Operation(summary = "获取城市所有天气信息", description = "根据城市代码获取所有天气相关信息，包括当前天气、预报和警报")
    public Mono<Result<Map<String, Object>>> getAllWeatherInfo(
            @Parameter(description = "城市代码", required = true)
            @PathVariable String cityCode) {
        return weatherService.getAllWeatherInfo(cityCode);
    }

    @GetMapping("/cities")
    @Operation(summary = "获取所有城市代码和名称", description = "获取系统中所有支持的城市列表")
    public Mono<Result<List<Map<String, Object>>>> getAllCities() {
        return weatherService.getAllCities();
    }

    @GetMapping("/search")
    @Operation(summary = "根据城市名称搜索", description = "通过城市名称关键字搜索匹配的城市及其天气信息")
    public Mono<Result<List<WeatherCurrent>>> searchByName(
            @Parameter(description = "城市名称", required = true)
            @RequestParam String name) {
        return weatherService.searchByName(name);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取天气统计信息", description = "获取系统中的天气统计数据")
    public Mono<Result<Map<String, Object>>> getStatistics() {
        return weatherService.getStatistics();
    }

    @GetMapping("/hot-cities")
    @Operation(summary = "获取热门城市天气", description = "获取温度最高的城市列表及其天气信息")
    public Mono<Result<List<WeatherCurrent>>> getHotCities() {
        return weatherService.getHotCities();
    }

    @GetMapping("/cold-cities")
    @Operation(summary = "获取最冷城市天气", description = "获取温度最低的城市列表及其天气信息")
    public Mono<Result<List<WeatherCurrent>>> getColdCities() {
        return weatherService.getColdCities();
    }

    @GetMapping("/temperature-range")
    @Operation(summary = "获取特定温度范围内的城市", description = "查询当前温度在指定范围内的所有城市")
    public Mono<Result<List<WeatherCurrent>>> getCitiesByTemperatureRange(
            @Parameter(description = "最低温度", required = true)
            @RequestParam BigDecimal minTemp,
            @Parameter(description = "最高温度", required = true)
            @RequestParam BigDecimal maxTemp) {
        return weatherService.getCitiesByTemperatureRange(minTemp, maxTemp);
    }

    @GetMapping("/forecast/date/{cityCode}")
    @Operation(summary = "获取指定日期的天气预报", description = "根据城市代码和日期获取特定日期的天气预报")
    public Mono<Result<WeatherForecast>> getForecastByDate(
            @Parameter(description = "城市代码", required = true)
            @PathVariable String cityCode,
            @Parameter(description = "预报日期(yyyy-MM-dd)", required = true)
            @RequestParam String date) {
        return weatherService.getForecastByDate(cityCode, date);
    }

    @GetMapping("/weather-condition/{condition}")
    @Operation(summary = "获取特定天气状况的城市", description = "查询当前天气状况为指定条件的所有城市")
    public Mono<Result<List<WeatherCurrent>>> getCitiesByWeatherCondition(
            @Parameter(description = "天气状况(如：晴、多云、雨等)", required = true)
            @PathVariable String condition) {
        return weatherService.getCitiesByWeatherCondition(condition);
    }

    @PostMapping("/compare")
    @Operation(summary = "比较多个城市的天气", description = "对比多个城市的天气信息")
    public Mono<Result<Map<String, List<WeatherCurrent>>>> compareCities(
            @Parameter(description = "城市代码列表", required = true)
            @RequestBody List<String> cityCodes) {
        return weatherService.compareCities(cityCodes);
    }
} 